import 'dart:math';

import 'package:flutter/cupertino.dart';

/// 转为 rpx
double toRpx(BuildContext context, double size) {
  double rpx = MediaQuery.of(context).size.width / 750;
  return size * rpx;
}

///数字转换
String formatCharCount(int count) {
  if (count <= 10 || count.isNaN) {
    return '0';
  }
  String strCount = count.toString();
  if (strCount.length >= 5) {
    // 38128 => 2.8w
    // 381285 => 38.1w
    String prefix = strCount.substring(0, strCount.length - 4);

    if (strCount.length == 5) {
      prefix += '.${strCount[1]}';
    }
    if (strCount.length == 6) {
      prefix += '.${strCount[2]}';
    }
    return prefix + 'w';
  }
  return strCount;
}

///数组转换
List<String> formatStringArray(String stringList) {
  if (stringList.isEmpty) {
    return [];
  }
  if (stringList.contains(",")) {
    return stringList.split(',');
  }
  if (stringList.contains(';')) {
    return stringList.split(';');
  }
  if (stringList.contains(' ')) {
    return stringList.split(' ');
  }
  return [stringList];
}

/// 随机获取制定返回内的数值
///
/// from [min] 含，to [max] 不含
int getRandmonRangeInt(int min, int max) {
  final Random random = Random();
  return min + random.nextInt(max + 1 - min);
}

/// 秒数值转时分秒
String secondsToTime(int seconds) {
  if (seconds < 0 || seconds.isNaN) {
    return "00:00";
  }
  var d = Duration(seconds: seconds);
  String t = d.toString().split('.').first;
  List<String> parts = t.split(':');
  return '${parts[0]}:${parts[1]}:${parts[2]}';
}
